<template >
    <div class="repassword">
      <h1>修改密码</h1>
      <el-row justify="center">
        <el-col :sm="16" :md="12" :lg="8">
          <el-form class="repasswordform" ref="passForm" label-width="auto" :rules="rules" :model="passForm">
            <div class="logint">
              <h3 style="margin-bottom: 10px">欢迎!</h3>
            </div>
            <el-form-item prop="currentPass">
              <el-input type="password" placeholder="请输入原密码" v-model="passForm.currentPass"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" placeholder="请输入新密码" v-model="passForm.password"></el-input>
            </el-form-item>

            <el-form-item prop="checkPass">
              <el-input type="password" placeholder="请确认新密码" v-model="passForm.checkPass"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%" @click="submitForm('passForm')"
              >确认修改</el-button
              >
            </el-form-item>
          </el-form></el-col>
      </el-row>
    </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      userInfo: {

      },
      passForm: {
        password: '',
        checkPass: '',
        currentPass: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        currentPass: [
          { required: true, message: '请输入当前密码', trigger: 'blur' },
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      this.$axios.get("/sys/userInfo").then(res => {

        this.userInfo = res.data.data;
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          const _this = this
          this.$axios.post('/sys/user/updatePass', this.passForm).then(res => {

            _this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定',
              callback: action => {
                localStorage.clear()
                sessionStorage.clear()
                this.$store.commit("resetState")
                this.$router.replace("/login")
                location.reload()
              }
            });
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.repassword {
  width: 95%;
  border-radius: 20px;
  background-color: #e5e5e5;
  box-shadow: 5px 5px 5px rgba(153, 153, 153, 0.5);
  margin: 0 auto;
  padding-top: 20px;
  padding-bottom: 50px;
}
h1 {
  text-align: center;
  margin-bottom: 50px;
}
.repasswordform {
  margin: 0 auto;
  padding: 5%;
  background-color: rgba(255, 255, 255, 0.5);
}
.el-row{
  vertical-align: middle;
}

.el-form-item {
  height: 60px;
}
</style>